package logic

import (
	"context"
	"gorm.io/gorm"
	"stores/mysql"

	"stores/storesrpc/internal/svc"
	"stores/storesrpc/storesrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type StoreUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStoreUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StoreUpdateLogic {
	return &StoreUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *StoreUpdateLogic) StoreUpdate(in *storesrpc.StoreUpdateRequest) (*storesrpc.StoreUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var store mysql.Store
	mysql.DB.Debug().Where("id=?", in.Id).Find(&store)

	m := mysql.Store{
		Model:   gorm.Model{ID: uint(in.Id)},
		Name:    in.Name,
		Address: in.Address,
		Num:     in.Num,
	}
	mysql.DB.Debug().Updates(&m)
	return &storesrpc.StoreUpdateResponse{
		Id: int64(m.ID),
	}, nil
}
